package com.control.qct.businesssteward.activity;

import android.graphics.Color;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.base.BaseActivity;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 忘记密码
 * Created by Administrator on 2018/1/2 0002.
 */

public class ForgetPasswordActivity extends BaseActivity {
    @BindView(R.id.et_phone_number)
    EditText mEtPhoneNumber;
    @BindView(R.id.et_verification_code)
    EditText mEtVerificationCode;
    @BindView(R.id.tv_verification_code)
    TextView mTvVerificationCode;
    @BindView(R.id.et_new_password)
    EditText mEtNewPassword;
    @BindView(R.id.iv_show_new_password)
    ImageView mIvShowNewPassword;
    @BindView(R.id.et_sure_new_password)
    EditText mEtSureNewPassword;
    @BindView(R.id.iv_show_sure_password)
    ImageView mIvShowSurePassword;
    @BindView(R.id.tv_submit)
    TextView mTvSubmit;

    private int timeDown;
    private Timer mTimer;
    private TimerTask mTimerTask;
    private boolean isShow;
    private boolean isSureShow;

    public ForgetPasswordActivity() {
        super(R.layout.activity_forget_password);
    }

    @Override
    protected void initView() {
        setLeftIcon(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        setTitle("忘记密码");
    }

    @OnClick({R.id.tv_verification_code, R.id.tv_submit, R.id.iv_show_new_password, R.id.iv_show_sure_password})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_verification_code://获取验证码
                setTimerTask();
                break;
            case R.id.tv_submit://提交
                judgeEmpty();
                break;
            case R.id.iv_show_new_password://是否显示新密码
                setIsShowPassword(isShow, mIvShowNewPassword, mEtNewPassword);
                if (isShow) {
                    isShow = false;
                } else {
                    isShow = true;
                }
                break;
            case R.id.iv_show_sure_password://是否显示确认密码
                setIsShowPassword(isSureShow, mIvShowSurePassword, mEtSureNewPassword);
                if (isSureShow) {
                    isSureShow = false;
                } else {
                    isSureShow = true;
                }
                break;
        }
    }

    /**
     * 设置是否显示密码
     *
     * @param isShow
     * @param imageView
     * @param editText
     */
    private void setIsShowPassword(boolean isShow, ImageView imageView, EditText editText) {
        if (isShow) {//隐藏密码
            imageView.setImageResource(R.drawable.icon_denglu_yanjing);
            editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        } else {//显示密码
            imageView.setImageResource(R.drawable.icon_denglu_yanjing_s);
            editText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }
        editText.setSelection(editText.length());
    }

    /**
     * 设置倒计时
     */
    private void setTimerTask() {
        timeDown = 10;
        mTimer = new Timer();
        mTimerTask = new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        timeDown--;
                        mTvVerificationCode.setText("重新发送（" + timeDown + "秒" + "）");
                        mTvVerificationCode.setTextColor(Color.parseColor("#949494"));
                        mTvVerificationCode.setClickable(false);
                        if (timeDown == 0) {
                            mTvVerificationCode.setText("获取验证码");
                            mTvVerificationCode.setTextColor(Color.parseColor("#05C1AC"));
                            mTvVerificationCode.setClickable(true);
                            mTimer.cancel();
                            mTimer = null;
                        }
                    }
                });
            }
        };
        mTimer.schedule(mTimerTask, 100, 1000);
    }

    /**
     * 判断数据是否为空
     */
    private void judgeEmpty() {
        if (TextUtils.isEmpty(mEtPhoneNumber.getText().toString().trim())) {
            showShortToast("手机号码不能为空！");
            return;
        }
        if (TextUtils.isEmpty(mEtVerificationCode.getText().toString().trim())) {
            showShortToast("验证码不能为空！");
            return;
        }
        if (TextUtils.isEmpty(mEtNewPassword.getText().toString().trim())) {
            showShortToast("新密码不能为空！");
            return;
        }
        if (TextUtils.isEmpty(mEtSureNewPassword.getText().toString().trim())) {
            showShortToast("确认密码不能为空！");
            return;
        }
        if (!mEtSureNewPassword.getText().toString().trim().equals(mEtNewPassword.getText().toString().trim())) {
            showShortToast("两次密码输入不一致！");
            return;
        }
        setResult(RESULT_OK);
        finish();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mTimer != null) {
            mTimer.cancel();
            mTimer = null;
        }
    }
}
